package com.example.administrator.managesystem.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;


import com.example.administrator.managesystem.activity.my_profile.typein.TypeInCodeActivity;
import com.example.administrator.managesystem.listener.TipsClickListener;

import java.lang.ref.WeakReference;

public abstract class MyDialogFragment extends DialogFragment {

    protected String TAG = getClass().getName();

    protected final SparseArray<View> views = new SparseArray<>();

    protected View mParentView;

    public AppCompatActivity mActivity;
    private boolean mShareClick = true;

    protected StatusListener mDismissListener;

    protected ItemClickListener itemClickListener;

    public TipsClickListener tipsListener;


    protected void dealWith(Message msg) {

    }

    //获得可靠的上下文,防止getActivivty报空指针
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (AppCompatActivity) activity;
    }

    public AppCompatActivity getAttachActivity() {
        return mActivity;
    }


    @Override
    public void onStart() {
        super.onStart();
        // 让dialogFragment全屏显示
//        getDialog().getWindow().setBackgroundDrawable( new ColorDrawable(Color.WHITE) );
////        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
//        Dialog dialog = getDialog();
//        if (dialog != null) {
//            int width = ViewGroup.LayoutParams.MATCH_PARENT;
//            int height = ViewGroup.LayoutParams.MATCH_PARENT;
//            dialog.getWindow().setLayout(width, height);
//        }

    }

    @Override
    public void onDestroy() {

        super.onDestroy();

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mDismissListener != null) mDismissListener.dismiss();
    }

    public MyDialogFragment setText(@IdRes int viewId, CharSequence value) {
        TextView view = getView(viewId);
        if (view == null) return this;
        if (TextUtils.isEmpty(value)) value = "";
        view.setText(value);
        return this;
    }

    public MyDialogFragment setGone(@IdRes int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T getView(@IdRes int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = mParentView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 弹出Dialog dialogFragmnet 的show 和 hide
     *  @param activity
     * @param redDialog
     */
    public MyDialogFragment showDialog(AppCompatActivity activity, String redDialog) {
        try {

            if (activity == null) {
                return this;
            }
            if (activity.isFinishing()) {
                return this;
            }
            if (this.isAdded()) {
                return this;
            }

            if (this == null) {
                return this;
            }
            if (this.isVisible()) {
                return this;
            }
            WeakReference<AppCompatActivity> activityWeakReference = new WeakReference<>(activity);
            AppCompatActivity activityNew = activityWeakReference.get();
            FragmentManager fragmentManager = activityNew.getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(this, redDialog);
            transaction.commitAllowingStateLoss();
            if (mDismissListener != null) mDismissListener.show();
        } catch (Exception e) {

        }
        return this;

    }

    public void dismissDialog(AppCompatActivity activity) {
        try {
            WeakReference<AppCompatActivity> activityWeakReference = new WeakReference<>(activity);
            AppCompatActivity activityNew = activityWeakReference.get();
            if (activityNew == null) {

                return;
            }
            if (activityNew.isFinishing()) {

                return;
            }
            super.dismissAllowingStateLoss();
            if (mDismissListener != null) mDismissListener.dismiss();
        } catch (Exception e) {

        }
    }

    /**
     * 宽
     *
     * @return
     */
    public static int getWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 高
     *
     * @return
     */
    public static int getHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }


    /**
     * 创建Dialog
     *
     * @return
     */
    protected Dialog MyCreateDialog() {
        Dialog dialog = null;
        WeakReference<AppCompatActivity> activityWeakReference = new WeakReference<>(getAttachActivity());
        AppCompatActivity activity = activityWeakReference.get();
        if (activity != null) {
            dialog = new Dialog(activity);
        } else {
            dialog = new Dialog(getAttachActivity());
        }
        return dialog;
    }

    /**
     * 创建Dialog
     *
     * @return
     */
    protected Dialog MyCreateDialog(int style) {
        Dialog dialog = null;
        WeakReference<AppCompatActivity> activityWeakReference = new WeakReference<>(getAttachActivity());
        AppCompatActivity activity = activityWeakReference.get();
        if (activity != null) {
            dialog = new Dialog(activity, style);
        } else {
            dialog = new Dialog(getAttachActivity(), style);

        }
        return dialog;
    }

    public void setOnStatusListener(StatusListener listener) {
        this.mDismissListener = listener;
    }

    public interface StatusListener {

        void show();

        void dismiss();

    }

    public interface ItemClickListener {
        void onClick(int position);
    }

    public MyDialogFragment setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
        return this;
    }

    public MyDialogFragment setOnTipsClickListener(TipsClickListener listener) {
        this.tipsListener = listener;
        return this;
    }
}
